1 package com.fc.taglibs.castor;
2
3 import java.io.*;
4 import java.util.ArrayList;
5 import javax.naming.*;
6 import javax.servlet.jsp.*;
7 import javax.servlet.jsp.tagext.*;
8 import org.apache.taglibs.standard.lang.support.*;
9
10
11
12 import javax.naming.InitialContext;
13 import org.exolab.castor.jdo.DataObjects;
14 import org.exolab.castor.jdo.Database;
15 import org.exolab.castor.jdo.PersistenceException;
16 import org.exolab.castor.jdo.TransactionNotInProgressException;
17 import org.exolab.castor.jdo.QueryException;
18 import org.exolab.castor.jdo.OQLQuery;
19 import org.exolab.castor.jdo.QueryResults;
20
21 public class CastorQueryResultsTag extends BodyTagSupport implements IterationTag
22 {
23 /***
24 * The name of this tag
25 */
26 private static String _tagName = "CastorQueryResultsTag";
27
28 /***
29 * The id by which we will refer to the Results object in the JSP page.
30 */
31 private String id;
32
33 /***
34 * The size of the results page
35 */
36 private String size;
37
38 /***
39 * The current page
40 */
41 private String page;
42
43 /***
44 * The name of a variable containing the current index of the loop
45 */
46 private String indexId;
47
48 /***
49 * These two booleans are to ensure
50 * that paged results are set correctly
51 */
52 private int _size = 0;
53 private int _page =0;
54 private boolean indexIdSet = false;
55 private boolean sizeSet = false;
56 private boolean pageSet = false;
57 private boolean pagedResults = false;
58
59 /***
60 * For determining access mode
61 */
62 private boolean accessModeSet = false;
63 private short _accessMode = Database.Shared;
64 private String accessMode = null;
65
66 /***
67 * The castor objects we will need.
68 */
69 private Database db = null;
70 private OQLQuery query = null;
71 private QueryResults results = null;
72
73 int index = 0;
74
75 /***
76 * Sets the id by which this bean instance will be referred to in the page
77 */
78 public void setId(String _id)
79 {
80 this.id = _id;
81 }
82
83 /***
84 * Sets the size
85 */
86 public void setSize(String _size)
87 {
88 this.sizeSet = true;
89 this.size = _size;
90 }
91
92
93 /***
94 * Sets current result page
95 */
96 public void setPage(String _page)
97 {
98 this.pageSet = true;
99 this.page = _page;
100 }
101
102 /***
103 * Sets the name of the index int
104 */
105 public void setIndexId(String _indexId)
106 {
107 this.indexIdSet = true;
108 this.indexId = _indexId;
109 }
110
111 /***
112 * Sets the access mode string
113 */
114 public void setAccessMode(String _accessModeStr)
115 {
116 this.accessModeSet = true;
117 this.accessMode = _accessModeStr;
118 }
119
120
121 /***
122 * Do the start tag bit
123 */
124 public int doStartTag() throws JspException
125 {
126
127 try
128 {
129 /***
130 * Get the database from the enclosing transaction tag
131 * Throw an exception if outside one
132 */
133 CastorTransactionInitiator t = (CastorTransactionInitiator)findAncestorWithClass(this, CastorTransactionInitiator.class);
134 if (t!=null)
135 {
136 this.db = t.getDatabase();
137 }
138 else
139 {
140 String msg = "Tag not nested within a CastorTransactionTag.";
141 log(msg,null);
142 throw new Exception(msg);
143 }
144
145 /***
146 * Get the Oql query from the parent CastorOqlTag
147 */
148 CastorOqlTag parent = (CastorOqlTag)findAncestorWithClass(this, CastorOqlTag.class);
149 if (parent!=null)
150 {
151 query = parent.getOql();
152 }
153 else
154 {
155 String msg = "Tag must be used within CastorOqlTag.";
156 log(msg,null);
157 throw new Exception(msg);
158 }
159
160 /***
161 * Determine the accessmode
162 */
163 if (this.accessModeSet)
164 {
165 if (this.accessMode.equals("DbLocked"))
166 this._accessMode = Database.DbLocked;
167 else if (this.accessMode.equals("Exclusive"))
168 this._accessMode = Database.Exclusive;
169 else if (this.accessMode.equals("Shared"))
170 this._accessMode = Database.Shared;
171 else if (this.accessMode.equals("ReadOnly"))
172 this._accessMode = Database.ReadOnly;
173 else
174 {
175 String msg = "Invalid Access Mode set.";
176 log(msg , null);
177 throw new JspTagException(msg);
178 }
179 }
180
181
182 /***
183 * Check whether results will be paged
184 */
185 if (pageSet && sizeSet)
186 pagedResults=true;
187 else if (!pageSet && !sizeSet)
188 pagedResults=false;
189 else
190 {
191 String msg = "Both size and page must be set, or neither.";
192 log(msg , null);
193 throw new JspTagException(msg);
194 }
195 /***
196 * Evaluate expressions for page and size (in case EL values are used)
197 */
198 if (pagedResults)
199 {
200 _page = ((Integer)ExpressionEvaluatorManager.evaluate("page",this.page, Integer.class, this, pageContext)).intValue();
201 _size = ((Integer)ExpressionEvaluatorManager.evaluate("size",this.size, Integer.class, this, pageContext)).intValue();
202 }
203
204
205 /***
206 * Execute the query
207 */
208 results = query.execute(_accessMode);
209
210
211 /***
212 * Find out what part of the result set to display
213 */
214 if (pagedResults)
215 {
216 if (this._page >1)
217 {
218 //we are on a later page so move to the relevent row in the result set
219 int currentRow = ((this._page - 1) * this._size);
220 results.absolute(currentRow);
221 }
222 }
223
224 //ensure that index is initialised to zero
225 index=0;
226
227 //perform the first iteration
228 if ((results.hasMore() && !pagedResults)
229 || (results.hasMore() && pagedResults && (index < this._size)))
230 {
231 pageContext.setAttribute(this.id, results.next());
232 if (indexIdSet)
233 pageContext.setAttribute(this.indexId, new Integer(index));
234 //increment index
235 index++;
236 return EVAL_BODY_INCLUDE;
237 }
238 else
239 {
240 //don't iterate, skip body
241 return SKIP_BODY;
242 }
243
244
245 }
246
247 catch(QueryException e)
248 {
249 log(e.getMessage() , e);
250 throw new JspTagException(e.getMessage());
251 }
252 catch(TransactionNotInProgressException e)
253 {
254 log(e.getMessage() , e);
255 throw new JspTagException(e.getMessage());
256 }
257 catch(PersistenceException e)
258 {
259 log(e.getMessage() , e);
260 throw new JspTagException(e.getMessage());
261 }
262 catch(Exception e)
263 {
264 log(e.getMessage() , e);
265 throw new JspTagException(e.getMessage());
266 }
267 }
268
269 public int doAfterBody() throws JspException
270 {
271 try
272 {
273 if ((results.hasMore() && !pagedResults)
274 || (results.hasMore() && pagedResults && (index < this._size)))
275 {
276 pageContext.setAttribute(this.id, results.next());
277 if (indexIdSet)
278 pageContext.setAttribute(this.indexId, new Integer(index));
279 //increment index
280 index++;
281 return EVAL_BODY_AGAIN;
282 }
283 else
284 {
285 return SKIP_BODY;
286 }
287 }
288 catch(Exception e)
289 {
290 log(e.getMessage() , e);
291 throw new JspTagException(e.getMessage());
292 }
293 //return SKIP_BODY;
294 }
295
296
297 public int doEndTag() throws JspException
298 {
299 return EVAL_PAGE;
300 }
301
302
303
304 private void log(String msg,Exception e)
305 {
306 if (e!=null)
307 {
308 System.err.println(_tagName + ": " + msg);
309 e.printStackTrace();
310 }
311 else
312 {
313 System.out.println(_tagName + ": " + msg);
314 }
315 }
316
317
318 }
This page was automatically generated by Maven